import urllib2
import re
import simplejson

import parse_html.py

def formatOutJson(plain_json):
    json_obj = simplejson.loads(plain_json)
    for element in json_obj:
        print '%s\t%s'%(element, json_obj[element])

def searchJson(page_content):
    page_num = 0
    regex = re.compile('STK && STK\.pageletM && STK\.pageletM\.view\((.*)\)')
    for mat in regex.finditer(page_content):
        json_obj = simplejson.loads(mat.group(1))
        if not filerJson(json_obj):
            continue
        else:
            page_num = parse_html.getPageCnt(json_obj['html'])
            if page_num == 0:
                continue
            else:
                break
    return page_num


def filterJson(json_obj):
    if json_obj['html'] is None:
        return False

def loadPage(url):
    page = urllib2.urlopen(url).read()
    return searchJson(page)

def writePage(url, name):
    page = urllib2.urlopen(url).read()
    file_obj = open(name, 'w')
    file_obj.write(page)
    file_obj.close()

def fetch_main(uid):
    url = 'http://www.weibo.com/'+uid+'/follow'
    page_num = loadPage(url)
    for i in range(1, page_num):
        url = 'http://www.weibo.com/'+uid+'/follow?page='+str(i)
        writePage(url, uid+'_follow'+str(i))

    url = 'http://www.weibo.com/'+uid+'/fans'
    page_num = loadPage(url)
    for i in range(1, page_num):
        url = 'http://www.weibo.com/'+uid+'/fans?page='+str(i)
        write(url, uid+'_friend'+str(i))
    
    url = 'http://www.weibo.com/u/'+uid
    write(url, uid)

if __name__ == '__main__':
    main()
